/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

Dialog {
    id: loginrect
    width: 400
    height: 300
    label:  "Sign in to Carpool "

    property string datacloudurl: ""
    property string username: ""
    property string password: ""
    property string karpoid: ""

    property string firstname: ""
    property string lastname: ""
    property string hometown: ""
    property string phone: ""
    property string alias: ""


    property string showname: "0"
    property string showphone: "0"
    property string showhometown: "0"

    property bool newuser: false

    signal loginOk
    signal loginCancel



    function getData()
    {
        var temp = {}
        temp["username"]=username;
        temp["password"]=password;
        temp["karpoid"]=karpoid;

        temp["firstname"]=firstname;
        temp["lastname"]=lastname;
        temp["hometown"]=hometown;
        temp["phone"]=phone;
        temp["alias"]=alias;

        return temp;
    }

    function setData(userdata)
    {
        try
        {
            username = userdata["username"]
            password = userdata["password"]
            karpoid = userdata["karpoid"]

            firstname = userdata["firstname"];
            lastname = userdata["lastname"];
            hometown = userdata["hometown"];
            phone = userdata["phone"];
            alias = userdata["alias"];


            usernameinput.text = username
            passwordinput.text = password


        }
        catch (e)
        {
            console.log("Login::setData exception: " + e)
        }


    }


    function logincb(response)
    {
        try
        {
            console.log("-- RESPONSE\r\n" + JSON.stringify(response) + "\r\n--------------")

            if (response.isError)
            {
                messagebox.text = response.errorString
            }
            else
            {
                // login ok
                messagebox.visible = false

                try
                {
                    loginrect.karpoid = response.karpoid
                    loginrect.password = response.password
                    loginrect.username = response.username
                    loginrect.karpoid = response.karpoid
                    loginrect.firstname = response.firstname
                    loginrect.lastname = response.lastname
                    loginrect.hometown = response.hometown
                    loginrect.phone = response.phone
                    loginrect.alias = response.alias
                    loginrect.showname = (response.showname?"1":"0")
                    loginrect.showphone = (response.showphone?"1":"0")
                    loginrect.showhometown = (response.showhometown?"1":"0")
                }
                catch (e)
                {
                    console.log("exception = " + e)
                }

                if (deleteusercheckbox.selected)
                {
                    messagebox.text = "Do you want to delete user's<br>" + username + " account?"
                    messagebox.onOkTodo = "deleteuseraccount"
                    messagebox.show("confirmation")
                }
                else
                {
                    loginrect.loginOk()
                }
            }
        }
        catch (e)
        {
            console.log("logincb excep: "+ e);
        }
    }

    onOk:
    {
        if (usernameinput.text.length == 0 || passwordinput.text.length == 0)
        {
            messagebox.text = "Check the username and password!"
            messagebox.visible = true
        }
        else if (passwordconfirmationinput.text.length > 0 && passwordinput.text != passwordconfirmationinput.text)
        {
            messagebox.text = "Check the password!"
            messagebox.visible = true
        }
        else
        {

            newuser = (passwordconfirmationinput.text.length > 0)

            var dcpersonalprofile = new DCDATA.DCPersonalProfile()
            dcpersonalprofile.username = usernameinput.text
            dcpersonalprofile.password = passwordinput.text
            dcpersonalprofile.confirmpassword = passwordconfirmationinput.text
            dcpersonalprofile.deleteaccount = deleteusercheckbox.selected


            if (deleteusercheckbox.selected && newuser)
            {
                messagebox.text = "Can't delete user account<br>if it is not yet created."
                messagebox.visible = true
            }
            else
            {
                messagebox.text = "Signing in to Carpool ..."
                messagebox.visible = true

                DCDATA.send(dcpersonalprofile, datacloudurl, "login", logincb)
            }
        }
    }

    onCancel:
    {
        loginCancel();
    }





    Rectangle
    {
        id: usernamerect
        color: "#00000000"
        height: 26
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: loginrect.titlerect.bottom
        anchors.topMargin: 2

        Text {
            id: usernameid
            width: 110
            text: "User name"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: usernamerect.verticalCenter
            font.pixelSize: 14
        }

        Rectangle {
            id: usernameinputrectangle
            height: 26
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: usernameid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: usernameinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: 14
            }
        }

    }

    Rectangle
    {
        id: passwordrect
        color: "#00000000"
        height: 26
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: usernamerect.bottom
        anchors.topMargin: 2

        Text {
            id: passwordid
            width: 110
            text: "Password"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: passwordrect.verticalCenter
            font.pixelSize: 14
        }

        Rectangle {
            id: passwordinputrectangle
            height: 26
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: passwordid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: passwordinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: 14
            }
        }

    }

    Rectangle {
        id: deleteuserrect
        height: 26
        color: "#00000000"
        opacity: 1
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.top: passwordrect.bottom
        anchors.topMargin: 2

        CheckBox {
            id: deleteusercheckbox
            label: "Delete account"
            anchors.left: parent.left
            anchors.leftMargin: 110
        }
    }

    Rectangle {
        id: passwordconfirmationrect
        height: 100
        anchors.top: deleteuserrect.bottom
        anchors.topMargin: 10
        gradient: Gradient {
            GradientStop {
                position: 0
                color: "#ffffff"
            }

            GradientStop {
                position: 1
                color: "#1e90ff"
            }
        }
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.left: parent.left
        anchors.leftMargin: 10



        Text {
            id: helptextid
            width: 200
            height: 26
            text: "Confirm your password if you are a new user"
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 10
            font.pixelSize: 14
        }

        Text {
            id: passwordconfirmationid
            width: 100
            height:  26
            text: "Password"
            anchors.top: helptextid.bottom
            anchors.topMargin: 15
            anchors.left: parent.left
            anchors.leftMargin: 10
            font.pixelSize: 14
        }

        Rectangle {
            id: passwordconfirmaioninputrectangle
            height: 26
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: passwordconfirmationid.right
            anchors.leftMargin: 0
            anchors.top: helptextid.bottom
            anchors.topMargin: 15
            border.color: "#000901"

            TextInput {
                id: passwordconfirmationinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                font.pixelSize: 14

            }
        }
    }

    function deleteuseraccount()
    {
        console.log("LOGIN::deletuseraccount(): " + username + ", " + karpoid)
        messagebox.text = "Deleting user account...";
        messagebox.show("");

        var dcpersonalprofile = new DCDATA.DCPersonalProfile()
        dcpersonalprofile.karpoid = loginrect.karpoid
        dcpersonalprofile.username = loginrect.username
        dcpersonalprofile.password = loginrect.password

        DCDATA.send(dcpersonalprofile, datacloudurl, "deleteuseraccount", deleteuseraccountcb)

    }

    function deleteuseraccountcb(response)
    {
        console.log("LOGIN::deleteuserprofilecb")
        if (response.isError)
        {
            messagebox.text = response.errorString
        }
        else
        {
            messagebox.text = "User account deleted."
            messagebox.show("")
        }
    }


    MessageBox
    {
        id: messagebox
        property string onOkTodo: "" // deleteuseraccount

        onOk:
        {
            if (onOkTodo == "deleteuseraccount")
            {
                deleteuseraccount()
            }

            onOkTodo = ""
        }

        onCancel:
        {
            onOkTodo = ""
        }

        function showInfo(msg)
        {
            messagebox.text = msg
            messagebox.show("")
        }
    }


}
